package com.tourism.website.comment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.tourism.website.comment.entity.Comment;
import com.tourism.website.comment.mapper.CommentMapper;
import com.tourism.website.comment.service.ICommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tourismwebsite.articledisplay.dto.CategoryDTO;
import com.tourismwebsite.comment.dto.CommentDTO;
import com.tourismwebsite.comment.query.CommentQuery;

import org.mapstruct.Mapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lyc
 * @since 2024-06-13
 */
@Mapper(componentModel="spring")
interface MsCommentMapper{
    CommentDTO CommentToCommentDTO(Comment comment);
}
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {
    @Resource
    MsCommentMapper msCommentMapper;
    @Override
    public List<CommentDTO> queryNotDelComments(CommentQuery commentQuery) {
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Comment::getArticleId,commentQuery.getArticleId());
        wrapper.eq(Comment::getDelFlag,0);
        wrapper.select(Comment::getCommentId,Comment::getUserId,Comment::getCreateTime,Comment::getContent);
        List<Comment> comments = baseMapper.selectList(wrapper);
        List<CommentDTO> commentDTOS = comments.stream()
                .map(comment -> msCommentMapper.CommentToCommentDTO(comment))
                .collect(Collectors.toList());
        return commentDTOS;
    }

    @Override
    public boolean saveComment(CommentQuery commentQuery) {
        Comment comment = new Comment();
        comment.setArticleId(commentQuery.getArticleId());
        comment.setContent(commentQuery.getContent());
        comment.setUserId(commentQuery.getUserId());
        comment.setDelFlag("0");
        comment.setCreateTime(LocalDateTime.now());
        int count = baseMapper.insert(comment);

        if (count > 0) {
            return true;
        } else {
            return false;
        }

    }

    @Override
    public boolean deleteComment(CommentQuery commentQuery) {
        LambdaUpdateWrapper<Comment> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Comment::getArticleId, commentQuery.getArticleId());
        wrapper.eq(Comment::getUserId,commentQuery.getUserId());
        wrapper.eq(Comment::getCommentId,commentQuery.getCommentId());
        int count = baseMapper.delete(wrapper);
        if (count > 0) {
            return true;
        } else {
            return false;
        }
    }
}
